#
# Makefile for the ESP Package Manager (EPM).
#
# Copyright © 1999-2020 by Michael R Sweet
# Copyright © 1999-2010 by Easy Software Products, all rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#

# Programs...
AR		=	@AR@
CC		=	@CC@
CHMOD		=	@CHMOD@
CODE_SIGN	=	@CODE_SIGN@
CP		=	@CP@
CXX		=	@CXX@
FLTKCONFIG	=	@FLTKCONFIG@
INSTALL		=	@INSTALL@
MKDIR		=	@MKDIR@ -p
RANLIB		=	@RANLIB@
RM		=	@RM@ -f
SHELL		=	/bin/sh
STRIP		=	@STRIP@


# Program options...
ARFLAGS		=	@ARFLAGS@
ARCHFLAGS	=	@ARCHFLAGS@
CFLAGS		=	$(ARCHFLAGS) @CFLAGS@ $(OPTIM)
CODESIGN_IDENTITY =	Developer ID
CPPFLAGS	=	@CPPFLAGS@
CXXFLAGS	=	$(ARCHFLAGS) @CXXFLAGS@ $(OPTIM)
GUILIBS		=	@GUILIBS@
LDFLAGS		=	$(ARCHFLAGS) @LDFLAGS@ $(OPTIM)
LIBS		=	@LIBS@
OPTIM		=	@OPTIM@


# Directories...
bindir		=	@bindir@
datadir		=	@datadir@
datarootdir	=	@datarootdir@
exec_prefix	=	@exec_prefix@
includedir	=	@includedir@
infodir		=	@infodir@
libdir		=	@libdir@
libexecdir	=	@libexecdir@
localstatedir	=	@localstatedir@
mandir		=	@mandir@
oldincludedir	=	@oldincludedir@
prefix		=	@prefix@
privateinclude	=	@privateinclude@
sbindir		=	@sbindir@
sharedstatedir	=	@sharedstatedir@
srcdir		=	@srcdir@
sysconfdir	=	@sysconfdir@
top_srcdir	=	@top_srcdir@

BUILDROOT	=	$(DSTROOT)$(RPM_BUILD_ROOT)$(DESTDIR)
VPATH		=	$(srcdir)


# Rules...
.SILENT:

.SUFFIXES:	.c .cxx .h .o
.c.o:
	echo Compiling $<...
	$(CC) $(CFLAGS) $(CPPFLAGS) -I. -I$(srcdir) -c $<
.cxx.o:
	echo Compiling $<...
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -I. -I$(srcdir) -c $<


# Targets...
TARGETS		=	libepm.a \
			epm \
			epminstall \
			mkepmlist \
			@GUIS@
EPM_OBJS	=	bsd.o \
			deb.o \
			dist.o \
			file.o \
			macos.o \
			portable.o \
			qprintf.o \
			rpm.o \
			run.o \
			snprintf.o \
			string.o \
			support.o \
			tar.o
SETUP_OBJS	=	setup.o \
			setup2.o \
			gui-common.o
UNINST_OBJS	=	uninst.o \
			uninst2.o \
			gui-common.o
OBJS		=	epm.o \
			$(EPM_OBJS) \
			epminstall.o \
			mkepmlist.o \
			$(SETUP_OBJS) \
			$(UNINST_OBJS)


# Make all targets...
all:	$(TARGETS) Makefile config.h


# Clean all generated files...
clean:
	$(RM) $(OBJS)
	$(RM) $(TARGETS)


# Clean all generated and configuration files...
distclean:
	$(RM) $(OBJS)
	$(RM) $(TARGETS)
	$(RM) config.cache config.h config.log config.status
	$(RM) Makefile doc/Makefile
	$(RM) epm.list


# Install all targets...
install: all @INSTALL_GUIS@ @INSTALL_OSX@
	echo Installing EPM programs in $(BUILDROOT)$(bindir)
	$(INSTALL) -d -m 755 $(BUILDROOT)$(bindir)
	for file in epm epminstall mkepmlist; do \
		$(INSTALL) -c -m 755 $$file $(BUILDROOT)$(bindir); \
	done
	(cd doc; $(MAKE) $(MFLAGS) install)

install-guis:	setup uninst
	echo Installing EPM setup/uninst in $(BUILDROOT)$(libdir)/epm
	$(INSTALL) -d -m 755 $(BUILDROOT)$(libdir)/epm
	for file in setup uninst; do \
		$(INSTALL) -c -m 755 $$file $(BUILDROOT)$(libdir)/epm; \
	done

install-macos:
	echo Installing EPM macOS data files in $(BUILDROOT)$(datadir)/epm
	$(INSTALL) -d -m 755 $(BUILDROOT)$(datadir)/epm
	$(INSTALL) -c -m 644 default.icns $(BUILDROOT)$(datadir)/epm


# Uninstall all targets...
uninstall:
	echo Uninstalling EPM programs from $(BUILDROOT)$(bindir)
	$(RM) $(BUILDROOT)$(bindir)/epm
	$(RM) $(BUILDROOT)$(bindir)/epminstall
	$(RM) $(BUILDROOT)$(bindir)/mkepmlist
	echo Uninstalling EPM setup/uninstall from $(BUILDROOT)$(libdir)/epm
	$(RM) -r $(BUILDROOT)$(libdir)/epm
	echo Uninstalling EPM macOS data files from $(BUILDROOT)$(datadir)/epm
	$(RM) -r $(BUILDROOT)$(datadir)/epm


# Makefile
Makefile:	Makefile.in configure
	if test -f config.status; then \
		./config.status --recheck; \
		./config.status; \
	else \
		./configure; \
	fi
	touch config.h


# config.h
config.h:	config.h.in configure
	if test -f config.status; then \
		./config.status --recheck; \
		./config.status; \
	else \
		./configure; \
	fi
	touch config.h


# Test EPM...
test:	$(TARGETS)
	echo Starting portable distribution build test...
	if ./epm -vv epm >test.log; then \
		echo Portable distribution build test PASSED.; \
	else \
		echo Portable distribution build test FAILED.; \
		cat test.log; \
	fi
	echo Starting native distribution build test...
	if ./epm -vv -s default.png -f native \
		--setup-program setup --uninstall-program uninst \
		epm >test.log; then \
		echo Native distribution build test PASSED.; \
	else \
		echo Native distribution build test FAILED.; \
		cat test.log; \
	fi
	$(RM) test.log


# Make distributions in different formats using EPM...
bsd: $(TARGETS)
	./epm -f bsd -v epm

deb: $(TARGETS)
	./epm -f deb -v epm

gui: $(TARGETS)
	./epm -f portable -v -s default.png --setup-program setup \
		--uninstall-program uninst --data-dir . epm

lsb macos portable rpm: $(TARGETS)
	./epm -f $@ -v -s default.png --setup-program setup \
		--uninstall-program uninst --data-dir . epm

native: $(TARGETS)
	./epm -f native -v epm


# libepm.a
libepm.a:	$(EPM_OBJS)
	echo Archiving libepm.a...
	$(RM) libepm.a
	$(AR) $(ARFLAGS) libepm.a $(EPM_OBJS)
	$(RANLIB) libepm.a

$(EPM_OBJS):	epm.h epmstring.h


# epm
epm:	epm.o libepm.a
	echo Linking epm...
	$(CC) $(LDFLAGS) -o epm epm.o libepm.a $(LIBS)
	echo Code signing $@...
	$(CODE_SIGN) -s "$(CODESIGN_IDENTITY)" $@

epm.o:	epm.h epmstring.h


# epminstall
epminstall:	epminstall.o libepm.a
	echo Linking epminstall...
	$(CC) $(LDFLAGS) -o epminstall epminstall.o libepm.a $(LIBS)
	echo Code signing $@...
	$(CODE_SIGN) -s "$(CODESIGN_IDENTITY)" $@

epminstall.o:	epm.h epmstring.h


# mkepmlist
mkepmlist:	mkepmlist.o libepm.a
	echo Linking mkepmlist...
	$(CC) $(LDFLAGS) -o mkepmlist mkepmlist.o libepm.a $(LIBS)
	echo Code signing $@...
	$(CODE_SIGN) -s "$(CODESIGN_IDENTITY)" $@

mkepmlist.o:	epm.h epmstring.h


# setup (GUI)
setup:	$(SETUP_OBJS) libepm.a
	echo Linking setup...
	$(CXX) $(LDFLAGS) -o setup $(SETUP_OBJS) libepm.a $(GUILIBS) $(LIBS)

setup.o:	setup.h gui-common.h epmstring.h
setup2.o:	setup.h gui-common.h epmstring.h
gui-common.o:	gui-common.h


# uninst (GUI)
uninst:	$(UNINST_OBJS) libepm.a
	echo Linking uninst...
	$(CXX) $(LDFLAGS) -o uninst $(UNINST_OBJS) libepm.a $(GUILIBS) $(LIBS)

uninst.o:	uninst.h epmstring.h gui-common.h
uninst2.o:	uninst.h epmstring.h gui-common.h
gui-common.o:	gui-common.h


# Common dependencies...
$(OBJS):	Makefile config.h
